package model

import (
	"time"
	"gorm.io/gorm"
)

// User 用户模型
type User struct {
	ID        uint           `json:"id" gorm:"primarykey"`
	Username  string         `json:"username" gorm:"uniqueIndex;size:50;not null" binding:"required"`
	Password  string         `json:"-" gorm:"size:100;not null" binding:"required"`
	Name      string         `json:"name" gorm:"size:100"`
	Email     string         `json:"email" gorm:"size:100"`
	Status    int8           `json:"status" gorm:"default:1"` // 1:启用 0:禁用
	CreatedAt time.Time      `json:"created_at"`
	UpdatedAt time.Time      `json:"updated_at"`
	DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
	
	// 关联关系
	Roles []Role `json:"roles" gorm:"many2many:user_roles;"`
}

// UserRole 用户角色关联表
type UserRole struct {
	UserID uint `json:"user_id" gorm:"primaryKey"`
	RoleID uint `json:"role_id" gorm:"primaryKey"`
}

// TableName 指定表名
func (User) TableName() string {
	return "users"
}

func (UserRole) TableName() string {
	return "user_roles"
}

// UserCreateRequest 创建用户请求
type UserCreateRequest struct {
	Username string `json:"username" binding:"required,min=3,max=50"`
	Password string `json:"password" binding:"required,min=6"`
	Name     string `json:"name" binding:"max=100"`
	Email    string `json:"email" binding:"omitempty,email"`
	Status   int8   `json:"status"`
	RoleIDs  []uint `json:"role_ids"`
}

// UserUpdateRequest 更新用户请求
type UserUpdateRequest struct {
	Name    string `json:"name" binding:"max=100"`
	Email   string `json:"email" binding:"omitempty,email"`
	Status  int8   `json:"status"`
	RoleIDs []uint `json:"role_ids"`
}

// UserResponse 用户响应
type UserResponse struct {
	ID        uint         `json:"id"`
	Username  string       `json:"username"`
	Name      string       `json:"name"`
	Email     string       `json:"email"`
	Status    int8         `json:"status"`
	CreatedAt time.Time    `json:"created_at"`
	UpdatedAt time.Time    `json:"updated_at"`
	Roles     []RoleSimple `json:"roles"`
}

// RoleSimple 简化的角色信息
type RoleSimple struct {
	ID   uint   `json:"id"`
	Name string `json:"name"`
}
